/**
 * Created by ajhin on 7/11/2015.
 */
package com.societyGames2.convert
{
import com.societyGames2.convert.TreeConverters;

import org.as3commons.asblocks.parser.antlr.LinkedListTree;

/***
 * Puts separated between each child.
 */
public class ListTreeConverter implements ITreeConverter
{
  private var _beginSource:String;
  private var _endSource:String;
  private var _separator:String;
  private var _shouldIncreaseTab:Boolean;

  public function ListTreeConverter(beginSource:String, endSource:String, separator:String=null, shouldIncreaseTab:Boolean=false)
  {
    _beginSource = beginSource;
    _endSource = endSource;
    _separator = separator;
    _shouldIncreaseTab = shouldIncreaseTab;
  }

  //ITreeToCSharp-->
  public function convert(tree:LinkedListTree):String
  {
    var source:String = "";
    if (!tree.isNil)
    {
      source += _beginSource;
    }
    if (_shouldIncreaseTab)
    {
      TreeConverters.pushTab();
    }
    for (var i:int = 0; tree.children != null && i < tree.children.length; i++)
    {
      var childTree:LinkedListTree = LinkedListTree(tree.getChild(i));
      var newSource:String = TreeConverters.convert(childTree);
      //Add spacer if we're past the first, have separator and it's not empty.
      if (i > 0 && _separator != null && newSource != null && newSource.length > 0)
      {
        source += _separator;
      }
      source += /*TreeConverters.getTabs() +*/ newSource;
    }
    if (_shouldIncreaseTab)
    {
      TreeConverters.popTab();
    }
    if (!tree.isNil)
    {
      source += _endSource;
    }
    return source;
  }
  //<--ITreeToCSharp
}
}
